/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-14 下午6:32 Created by dwzhang
 */
package com.jade.framework.base.util;

import java.util.Random;

/**
 * 产生唯一标识产生工具。
 *
 * @author dwzhang
 * @version 1.0 13-4-14 下午6:32
 */
public class UniqueID
{
    private static final String[] ASCII_ARRAY = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
                                                 "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
                                                 "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F",
                                                 "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
                                                 "U", "V", "W", "X", "Y", "Z"};
    private static final int DEFAULT_LENGTH = 6;

    private static Random factory = new Random();

    /**
     * Retunrn the next UUID, given the specified prefix.
     *
     * @return String - next unique id
     */
    public static final String nextString(String prefix)
    {
        StringBuilder sb = new StringBuilder(prefix.length() + 10);
        sb.append(factory.nextLong() & 0x7FFFFFFFFFFFFFFFL);
        return sb.toString();
    }

    /**
     * Return the next UUID
     *
     * @return String - next unique id
     */
    public static final String nextString()
    {
        return String.valueOf(factory.nextLong() & 0x7FFFFFFFFFFFFFFFL);
    }

    /**
     * Return the next UUID as a long
     *
     * @return long  - next as long
     */
    public static final long next()
    {
        return factory.nextLong() & 0x7FFFFFFFFFFFFFFFL;
    }

    /**
     * Return the next UUID as a long
     *
     * @return long  - next as long
     */
    public static final int nextInt()
    {
        return factory.nextInt() & 0x7FFFFFFF;
    }

    /**
     * Return the next UUID as a long
     *
     * @return long  - next as long
     */
    public static final int nextInt(int max)
    {
        return factory.nextInt(max);
    }


    /**
     * 创建默认位数的验证码
     * 所有位皆为数字
     * 默认位数为6
     */
    public static final String nextCode()
    {
        return nextCode(DEFAULT_LENGTH);
    }

    /**
     * 创建默认位数的验证码
     * 默认位数为6
     *
     * @param ascii 如果为true则包含字母
     */
    public static final String nextCode(boolean ascii)
    {
        return nextCode(DEFAULT_LENGTH, ascii);
    }

    /**
     * 创建指定位数的验证码
     * <p/>
     * 所有位皆为字母
     *
     * @param len 如果len <= 0 或 len > 0 则位数为默认的6
     */
    public static final String nextCode(int len)
    {
        if (len <= 0) {
            return nextCode();
        }
        StringBuilder code = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            code.append(ASCII_ARRAY[factory.nextInt(10)]);
        }
        return code.toString();
    }

    /**
     * 创建指定位数的验证码
     *
     * @param len   如果len <= 0 或 len > 10 则位数为默认的6
     * @param ascii 如果为true则包含字母
     */
    public static final String nextCode(int len, boolean ascii)
    {
        if (len <= 0) {
            return nextCode(ascii);
        }
        if (ascii) {
            StringBuilder code = new StringBuilder(len);
            for (int i = 0; i < len; i++) {
                code.append(ASCII_ARRAY[factory.nextInt(ASCII_ARRAY.length)]);
            }
            return code.toString();
        }
        else {
            return nextCode(len);
        }
    }
}
